package com.cjd.common.recycler;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cjd.common.utils.DisplayUtil;

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/9/27
 * description 防淘宝物流时间线 （左边）
 */
public class TimeLineItemDecoration extends RecyclerView.ItemDecoration {

    private int leftOffset;//左边偏移量
    private Paint circlePaint, linePaint;
    private int circleRadius;//圆圈 半径
    private RecyclerView.Adapter mAdapter;
    private int preColor = Color.parseColor("#1691F9");//选中之前颜色
    private int afterColor = Color.parseColor("#DADADA");
    private int selectedColor = Color.parseColor("#221691F9");//选中颜色
    private int selectedIndex;
    private int lrInterval;//左右间隔

    public TimeLineItemDecoration(RecyclerView.Adapter adapter, int selectedIndex) {
        this.mAdapter = adapter;
        this.selectedIndex = selectedIndex;
        circlePaint = new Paint();
        circlePaint.setAntiAlias(true);

        linePaint = new Paint();
        linePaint.setStrokeWidth(DisplayUtil.INSTANCE.dp2px(5));
        linePaint.setAntiAlias(true);

        circleRadius = DisplayUtil.INSTANCE.dp2px(8);
        lrInterval = DisplayUtil.INSTANCE.dp2px(15);
        leftOffset = lrInterval * 2 + circleRadius * 2;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.left = leftOffset;
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            int index = parent.getChildAdapterPosition(child);


            float top = child.getTop();
            float left = parent.getPaddingLeft();
            float bottom = child.getBottom();
            float right = parent.getWidth() - parent.getPaddingRight();

            float centerX = left + leftOffset / 2f;
            float centerY = top + (bottom - top) / 2f;

            centerX += lrInterval;
            centerX -= 10;


            //绘制选中圆 半透明
            if (index == selectedIndex) {
                circlePaint.setColor(selectedColor);
                c.drawCircle(centerX, centerY, circleRadius + 10, circlePaint);
            }

            if (index <= selectedIndex)
                circlePaint.setColor(preColor);
            else
                circlePaint.setColor(afterColor);

            //常规圆
            c.drawCircle(centerX, centerY, circleRadius, circlePaint);

            //绘制上半部分轴线 加1 用于缝合
            float upLineTopX = centerX;
            float upLineTopY = top;
            float upLineBottomX = centerX;
            float upLineBottomY = centerY - circleRadius + 1;
            if (index <= selectedIndex)
                linePaint.setColor(preColor);
            else
                linePaint.setColor(afterColor);

            if (index != 0)
                c.drawLine(upLineTopX, upLineTopY, upLineBottomX, upLineBottomY, linePaint);


            //绘制下半部分的轴线 减1 用于缝合
            float downLineTopX = centerX;
            float downLineTopY = centerY + circleRadius - 1;
            float downLineBottomX = centerX;
            float downLineBottomY = bottom;

            if (index < selectedIndex)//选中的下半部分 不绘制选中颜色
                linePaint.setColor(preColor);
            else
                linePaint.setColor(afterColor);

            if (index != mAdapter.getItemCount() - 1)
                c.drawLine(downLineTopX, downLineTopY, downLineBottomX, downLineBottomY, linePaint);

        }
    }
}
